import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false })

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  let token = localStorage.getItem('token')

  if (to.path == '/index/order' || to.path == '/index/mine') {
    token ? next() : next('/login')
  } else {
    next()
  }

  NProgress.start()

  document.title = to.meta && to.meta.title
})

router.afterEach((to, from) => {
  NProgress.done()
})

export default router
